package com.ruoyi.wq.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqUserFriend;
import com.ruoyi.wq.domain.WqUserFriendForm;
import com.ruoyi.wq.domain.vo.WqUserFriendVO;
import com.ruoyi.wq.mapper.WqUserFriendMapper;
import com.ruoyi.wq.service.IWqUserFriendService;

/**
 * 用户朋友Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-26
 */
@Service
public class WqUserFriendServiceImpl extends ServiceImpl<WqUserFriendMapper, WqUserFriend> implements IWqUserFriendService {
    
	@Autowired
    private WqUserFriendMapper wqUserFriendMapper;
	
	@Autowired
	private ISysUserService sysUserService;

    /**
     * 查询用户朋友
     * 
     * @param id 用户朋友主键
     * @return 用户朋友
     */
    @Override
    public WqUserFriend selectWqUserFriendById(String id)
    {
        return wqUserFriendMapper.selectWqUserFriendById(id);
    }

    /**
     * 查询用户朋友列表
     * 
     * @param wqUserFriend 用户朋友
     * @return 用户朋友
     */
    @Override
    public TableDataInfo selectWqUserFriendList(WqUserFriend wqUserFriend){
    	IPage<WqUserFriend> page = baseMapper.selectWqUserFriendList(Condition.getPage(wqUserFriend), wqUserFriend);
		List<WqUserFriend> list = page.getRecords();
		return Condition.getTableDataInfo(list, page.getTotal());
    }

    /**
     * 新增用户朋友
     * 
     * @param wqUserFriend 用户朋友
     * @return 结果
     */
    @Override
    public int insertWqUserFriend(WqUserFriend wqUserFriend)
    {
        wqUserFriend.setCreateTime(DateUtils.getNowDate());
        return wqUserFriendMapper.insertWqUserFriend(wqUserFriend);
    }

    /**
     * 修改用户朋友
     * 
     * @param wqUserFriend 用户朋友
     * @return 结果
     */
    @Override
    public int updateWqUserFriend(WqUserFriend wqUserFriend)
    {
        wqUserFriend.setUpdateTime(DateUtils.getNowDate());
        return wqUserFriendMapper.updateWqUserFriend(wqUserFriend);
    }

    /**
     * 批量删除用户朋友
     * 
     * @param ids 需要删除的用户朋友主键
     * @return 结果
     */
    @Override
    public int deleteWqUserFriendByIds(String[] ids)
    {
        return wqUserFriendMapper.deleteWqUserFriendByIds(ids);
    }

    /**
     * 删除用户朋友信息
     * 
     * @param id 用户朋友主键
     * @return 结果
     */
    @Override
    public int deleteWqUserFriendById(String id)
    {
        return wqUserFriendMapper.deleteWqUserFriendById(id);
    }

	@Override
	public boolean insertWqUserFriend(WqUserFriendForm wqUserFriendForm) {
		if(wqUserFriendForm.getUserId().equals(wqUserFriendForm.getFriendUserId())){
			return true;
		}
		SysUser friendUser = sysUserService.getById(wqUserFriendForm.getFriendUserId());
		if(friendUser == null){
			return true;
		}
		QueryWrapper<WqUserFriend> queryWrapper = new QueryWrapper<>();
		WqUserFriend entity = new WqUserFriend();
		entity.setUserId(wqUserFriendForm.getUserId());
		entity.setFriendUserId(wqUserFriendForm.getFriendUserId());
		queryWrapper.setEntity(entity);
		WqUserFriend userFriend = baseMapper.selectOne(queryWrapper);
		SysUser sysUser = sysUserService.getById(wqUserFriendForm.getUserId());
		if(userFriend == null){
			userFriend = new WqUserFriend();
			userFriend.setUserId(wqUserFriendForm.getUserId());
			userFriend.setAvatar(sysUser.getAvatar());
			userFriend.setNickName(sysUser.getNickName());
			
			userFriend.setFriendUserId(wqUserFriendForm.getFriendUserId());
			userFriend.setFriendAvatar(friendUser.getAvatar());
			userFriend.setFriendNickName(friendUser.getNickName());
			userFriend.setFriendSex(friendUser.getSex());
			userFriend.setFriendAge(friendUser.getAge());
			baseMapper.insert(userFriend);
		}
		
		QueryWrapper<WqUserFriend> query = new QueryWrapper<>();
		WqUserFriend friend = new WqUserFriend();
		friend.setUserId(wqUserFriendForm.getUserId());
		query.setEntity(friend);
		Integer fansNum = baseMapper.selectCount(query);
		sysUser.setFansNum(fansNum);
		sysUserService.updateById(sysUser);
		return true;
	}

	@Override
	public TableDataInfo getPageList(WqUserFriendVO wqUserFriend) {
		SysUser user = sysUserService.getById(SecurityUtils.getUserId());
		wqUserFriend.setLat(user.getLat());
		wqUserFriend.setLon(user.getLon());
		IPage<WqUserFriendVO> page = baseMapper.selectPageList(Condition.getPage(wqUserFriend), wqUserFriend);
		for(WqUserFriendVO vo : page.getRecords()){
			if("0".equals(vo.getWechatStatus()) && StringUtils.isNotBlank(vo.getWechat())){
				vo.setWechatFlag(true);
			}else{
				vo.setWechatFlag(false);
			}
			vo.setWechat(null);
			vo.setInviteCode(null);
    		vo.setDistance(String.valueOf(Arith.div(vo.getMi(), 1000.00, 2)));
        }
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public TableDataInfo getMyPageList(WqUserFriendVO wqUserFriend) {
		SysUser user = sysUserService.getById(SecurityUtils.getUserId());
		wqUserFriend.setLat(user.getLat());
		wqUserFriend.setLon(user.getLon());
		wqUserFriend.setUserId(user.getUserId());
		IPage<WqUserFriendVO> page = baseMapper.selectMyPageList(Condition.getPage(wqUserFriend), wqUserFriend);
		for(WqUserFriendVO vo : page.getRecords()){
			if("0".equals(vo.getWechatStatus()) && StringUtils.isNotBlank(vo.getWechat())){
				vo.setWechatFlag(true);
			}else{
				vo.setWechatFlag(false);
			}
			vo.setWechat(null);
			vo.setInviteCode(null);
    		vo.setDistance(String.valueOf(Arith.div(vo.getMi(), 1000.00, 2)));
        }
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public Long getCashByUserId(Long userId, String isStore) {
		String isPay = "1";
		if("1".equals(isStore)){
			isPay = "2";
		}
		Long data = baseMapper.getCashByUserId(userId, isPay);
		if(data == null){
			data = 0L;
		}
		return data;
	}
}
